#include <iostream>
#include <memory>
#include <string>

class Fruit
{
public:
    virtual void name() = 0;
};

class Apple : public Fruit
{
public:
    void name() override
    {
        std::cout << "I am an apple " << std::endl;
    }
};

class Banana : public Fruit
{
public:
    void name() override
    {
        std::cout << "I am a Banana " << std::endl;
    }
};

// class FruitFactor
// {
//     public:
//     static std::shared_ptr<Fruit> create(const std::string& s)
//     {
//         if(s == "Apple") return std::make_shared<Apple>();
//         else return std::make_shared<Banana>();
//     }
// };

class FruitFactory
{
public:
    virtual std::shared_ptr<Fruit> create() = 0;
};

class AppleFactory : public FruitFactory
{
public:
    std::shared_ptr<Fruit> create() override
    {
        return std::make_shared<Apple>();
    }
};

class BananaFactory : public FruitFactory
{
public:
    std::shared_ptr<Fruit> create() override
    {
        return std::make_shared<Banana>();
    }
};

int main()
{
    // std::shared_ptr<Fruit> sp1 = FruitFactor::create("Apple");
    // sp1->name();
    // std::shared_ptr<Fruit> sp2 = FruitFactor::create("Banana");
    // sp2->name();

    std::shared_ptr<FruitFactory> ff(new AppleFactory());
    std::shared_ptr<Fruit> fruit = ff->create();
    fruit->name();
    ff.reset(new BananaFactory());
    fruit = ff->create();
    fruit->name();
    return 0;
}